package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huiztech.maystall.R;
import com.huiztech.maystall.ui.PullDownView;
import com.huiztech.maystall.ui.PullDownView.OnPullDownListener;

public class CommentActivity extends BaseActivity implements
		OnPullDownListener, OnItemClickListener {

	private ListView listView;
	private PullDownView mPullDownView;
	private MeetAdapter meetAdapter;
	private List<Map<String, Object>> list;
	
	private Button backBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);

		backBtn = (Button) findViewById(R.id.comment_back_btn);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		list = new ArrayList<Map<String, Object>>();

		mPullDownView = (PullDownView) findViewById(R.id.list);
		mPullDownView.setOnPullDownListener(this);
		listView = mPullDownView.getListView();
		listView.setOnItemClickListener(this);
		meetAdapter = new MeetAdapter(this);
		listView.setAdapter(meetAdapter);

		getData();
	}

	private void getData() {
		for (int i = 0; i < 10; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("image", "image" + i);
			map.put("name", "name" + i);
			map.put("comment", "comment" + i);
			list.add(map);
		}

		meetAdapter.notifyDataSetChanged();
		mPullDownView.notifyDidLoad();
		mPullDownView.enableAutoFetchMore(false, 1);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	public void onRefresh() {
		mPullDownView.notifyDidRefresh();
	}

	@Override
	public void onMore() {
		mPullDownView.notifyDidMore();
	}

	class ViewHolder {
		ImageView imageView;
		TextView nameText;
		TextView commentText;
	}

	class MeetAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MeetAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;

			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.comment_list_item,
						null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.comment_item_image);
				holder.nameText = (TextView) convertView
						.findViewById(R.id.comment_item_name_text);
				holder.commentText = (TextView) convertView
						.findViewById(R.id.comment_item_comment_text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.imageView.setBackgroundResource(R.drawable.ic_launcher);

			if (list.get(position).get("name") != null) {
				holder.nameText.setText(list.get(position).get("name")
						.toString());
			}

			if (list.get(position).get("comment") != null) {
				holder.commentText.setText(list.get(position).get("comment")
						.toString());
			}

			return convertView;

		}
	}

}
